package cn.online.dao;

import java.util.List;

import cn.online.model.Problem;
import cn.online.util.Pagination;


public class ProblemDao extends GenericDao<Problem,Integer> {
	
	public List<Problem> getProblemByCourseId(Integer id){
		String hql = "from Problem as p where p.course.cid="+id;
		return getHibernateTemplate().find("from Problem as p where p.course.cid=?",new Object[]{id});
	}
	
	public List<Problem> getProblemByCourseId(Integer id,Integer isFinish){
		return getHibernateTemplate().find("from Problem as p where p.course.cid=? and p.pfinish=? ", new Object[]{id,isFinish});
	}
	
	public List<Problem> getNewProblems(){
		return getHibernateTemplate().find("from Problem as p  where  p.pfinish=? ", new Object[]{0});
	}
	
	public Pagination getPaginationByCourseId(Integer id, Integer status, int pageSize,
			int pageNumber) {
		StringBuffer sb = new StringBuffer();
		sb.append("from Problem as p where p.course.cid=");
		sb.append(id);
		if (status != null) {
			sb.append(" and p.pfinish=");
			sb.append(status);
		}

		int totalCount = ((Long) queryObject("select count(*) " + sb.toString(), null)).intValue();
		return getPagination(sb.toString() + " order by p.pid desc", null, pageSize, pageNumber,
				totalCount);
	}
}
